<script setup lang="ts">
import { reactive, ref } from 'vue'
import { loginApi } from '@/api/login'
import { useRouter } from 'vue-router'
import { setToken } from '@/utils/token'
import { User, Lock, View, Hide } from '@element-plus/icons-vue'

const userForm = reactive({
  username: '',
  password: ''
})

const router = useRouter()

const but_loading = ref(false)

const loginSimple = async () => {
  but_loading.value = true
  try {
    const res = await loginApi(userForm)

    setToken(res.result.token)
    but_loading.value = false
    router.push('/home')
  } catch (error) {
    but_loading.value = false
  }
}

const isShow = ref(false)

const toggleShow = () => {
  isShow.value = !isShow.value
}
</script>

<template>
  <div class="login">
    <div class="login-box">
      <h1>Login Form</h1>
      <el-form class="login-form">
        <el-form-item name="username">
          <el-input v-model="userForm.username" :prefix-icon="User"></el-input>
        </el-form-item>
        <el-form-item name="password">
          <el-input
            v-model="userForm.password"
            :type="isShow ? '' : 'password'"
            :prefix-icon="Lock"
            @paste.prevent="() => {}"
          >
            <template #suffix>
              <el-icon class="el-input__icon cs-pt">
                <component :is="isShow ? View : Hide" @click="toggleShow" />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
      </el-form>
      <el-button class="sub-but" type="success" @click="loginSimple" :loading="but_loading"
        >登录</el-button
      >
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login {
  height: 100%;
  display: flex;
  background: url('/src/assets/bg.webp') no-repeat 100% / cover;

  .login-box {
    width: 500px;
    height: 300px;
    margin: 15vh auto;
    padding: 40px 40px;
    text-align: center;
    background-color: rgba(0, 0, 0, 0.2);
    border-radius: 20px;
    box-shadow: 0 0 17px #aaa5a5;

    h1 {
      color: black;
      font-weight: bold;
    }

    .login-form {
      margin-top: 30px;
    }

    .sub-but {
      width: 100%;
    }
  }
}
</style>
